//
//  LBPageViewController.m
//  LBUIDEMO
//
//  Created by 刘文扬 on 16/8/9.
//  Copyright © 2016年 lianj. All rights reserved.
//

#import "LBPageViewController.h"
#import <Masonry.h>


#define IsView(_var) [_var isKindOfClass:[UIView class]]
#define IsViewController(_var) [_var isKindOfClass:[UIViewController class]]

@interface LBPageViewController ()<UIScrollViewDelegate>


@property(nonatomic,strong)NSMutableArray *controllers;

/** titleFontSize*/
@property(nonatomic,assign)CGFloat titleFontSize;
/** titleSelectFontSize*/
@property(nonatomic,assign)CGFloat titleSelectFontSize;

@end

@implementation LBPageViewController


-(instancetype)initWithViewOrViewControllers:(NSArray *)viewOrViewControllers titles:(NSArray *)titles;
{
    self = [super init];
    if (self) {
        _viewOrViewControllers = viewOrViewControllers;
        _titles = titles;
        _viewControllerClass = [UIViewController class];
        _isUnifyWidth = YES;
    }
    return self;
}

-(instancetype)initWithViewOrViewControllers:(NSArray *)viewOrViewControllers titles:(NSArray *)titles sliderConfig:(LBSliderConfig)sliderConfig
{
    self = [super init];
    if (self) {
        _viewOrViewControllers = viewOrViewControllers;
        _titles = titles;
        _sliderConfig = sliderConfig;
        _viewControllerClass = [UIViewController class];
        _isUnifyWidth = YES;
    }
    return self;
}

-(instancetype)initWithViewOrViewControllers:(NSArray *)viewOrViewControllers titles:(NSArray *)titles titleFontSize:(CGFloat)titleFontSize titleSelectFontSize:(CGFloat)titleSelectFontSize
{
    self = [super init];
    if (self) {
        _viewOrViewControllers = viewOrViewControllers;
        _titles = titles;
        _viewControllerClass = [UIViewController class];
        _isUnifyWidth = YES;
        _titleFontSize = titleFontSize;
        _titleSelectFontSize = titleSelectFontSize;
    }
    return self;
}

-(instancetype)initWithViewOrViewControllers:(NSArray *)viewOrViewControllers titles:(NSArray *)titles viewControllerClass:(Class)viewControllerClass
{
    self = [super init];
    if (self) {
        _viewOrViewControllers = viewOrViewControllers;
        _titles = titles;
        _viewControllerClass = viewControllerClass;
        _isUnifyWidth = YES;
    }
    return self;
}

-(NSArray *)arrayForControllerTitles
{
    return self.titles;
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.slideBar.itemSelectedColor = [UIColor colorWithRed:42.0/255.0 green:153.0/255.0 blue:241.0/255.0 alpha:1];
    self.slideBar.sliderColor = [UIColor colorWithRed:42.0/255.0 green:153.0/255.0 blue:241.0/255.0 alpha:1];
    self.slideBar.itemColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
    self.slideBar.titleFontSize = self.titleFontSize;
    self.slideBar.titleSelectFontSize = self.titleSelectFontSize;
    if (self.isUnifyWidth) {
        self.slideBar.isUnifyWidth = YES;
        self.slideBar.unifyWidth = [UIScreen mainScreen].bounds.size.width / self.titles.count;
        if (self.sliderConfig) {
            self.sliderConfig(self.slideBar);
        }
        [self reloadData];
    }

}

-(void)pageViewController:(id<ScrollPageViewControllerProtocol>)pageViewController didShowViewController:(UIViewController *)controller atIndex:(NSInteger)index
{
    if (self.didChangePageBlock) {
        self.didChangePageBlock(self.viewOrViewControllers[index],index);
    }
    
    self.vcInNavigationcontroller.tz_interactivePopDisabled = index == 0 ? NO : YES;
}


-(void)selectPageIndex:(NSInteger)page
{
    self.currentIndex = page;
    [self reloadData];
}

- (void)setScrollEnabled:(BOOL)scrollEnabled {
    _scrollEnabled = scrollEnabled;
    // pageViewController的滚动视图叫 @"_UIQueuingScrollView"
    for (id subView in self.pageViewController.view.subviews) {
        if ([NSStringFromClass([subView class]) containsString:@"UIQueuingScrollView"]) {
            UIScrollView *scrollView = (UIScrollView *)subView;
            scrollView.scrollEnabled = _scrollEnabled;
            break;
        }
    }
}

-(UIViewController *)viewcontrollerWithIndex:(NSInteger)index
{
    id viewOrViewController = self.viewOrViewControllers[index];
    
    if ([self.controllers[index] isKindOfClass:[UIViewController class]])
    {
        return self.controllers[index];
    }
    
    
    
    if (IsView(viewOrViewController)) {
        
        Class vcClass = self.viewControllerClass;
        
        UIViewController *vc = [[vcClass alloc] init];
        
        
        [vc.view addSubview:viewOrViewController];
        [viewOrViewController mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
        
        self.controllers[index] = vc;
        
        return vc;
    }
    
    self.controllers[index] = viewOrViewController;
    
    return viewOrViewController;
}

-(void)didClickShowMenu
{
    if (self.didClickShowBlock) {
        self.didClickShowBlock();
    }
}

-(void)setShowImage:(UIImage *)showImage
{
    self.slideBar.menuButtonImage = showImage;
}

-(void)dealloc
{
    NSLog(@"销毁了");
}

-(NSMutableArray<UIViewController *> *)controllers
{
    if (_controllers == nil) {
        _controllers = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < 20; ++i) {
            [_controllers addObject:@"1"];
        }
        
        
    }
    return _controllers;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
